// 活动列表
controllers.controller('bannerListCtrl', ['$scope', '$http', '$rootScope', '$modal', '$window', 'AlertService', 'Activity',
    function ($scope, $http, $rootScope, $modal, $window, AlertService, Activity) {

        $scope.resetCurrentPageName("活动维护");
        $scope.utils = Food.utils;
        $scope.pageSize = 20;
        $scope.currentPage = 1;
        $scope.activityList = Activity.activityList = [];

        $scope.queryList = function () {
            Activity.queryList($scope.title, $scope.startTime, $scope.endTime,
                $scope.currentPage, $scope.pageSize, function () {
                });
        };
        $scope.queryList();

        $scope.insertActivity = function () {
            Activity.insertActivity($scope.title, $scope.activityType, $scope.startTime, $scope.endTime, function () {

            });
        };
        $scope.initDetailActivity = function (activity) {

            Activity.currentActivity = activity;
            Activity.currentActivity.startTime = new Date(Activity.currentActivity.startTime);
            Activity.currentActivity.endTime = new Date(Activity.currentActivity.endTime);
            $window.location.href = "#activity/activityDetail";

        };
        $scope.deleteActivity = function (id) {
            Activity.deleteActivity(id, function () {

            });

        };
        var operateTemplate = "<button class='btn btn-primary btn-sm' ng-click='grid.appScope.initDetailActivity(row.entity)'>活动详情</button>" +
            "<button class='btn btn-error btn-sm' ng-click='grid.appScope.deleteActivity(row.entity.id)'>删除</button>";
        $scope.gridOptions = {
            data: 'activityList',
            enableColumnMenus: false,
            enableColumnResizing: true,
            rowHeight: 50,
            columnDefs: [

                {
                    field: 'title',
                    displayName: '标题',
                    cellTemplate: '<div>{{row.entity.title}}</div>'
                },
                {
                    field: 'startTime',
                    displayName: '开始时间',
                    cellFilter: "date:'yyyy-MM-dd hh:mm:ss'"
                },
                {
                    field: 'endTime',
                    displayName: '开始时间',
                    cellFilter: "date:'yyyy-MM-dd hh:mm:ss'"
                },
                {
                    field: 'id',
                    displayName: '操作',
                    cellTemplate: operateTemplate
                }
            ]
        };

    }]);

// 活动详情
controllers.controller('bannerDetailCtrl', ['$scope', '$http', '$rootScope', '$modal', '$window', 'AlertService', 'Activity', 'Upload',
    function ($scope, $http, $rootScope, $modal, $window, AlertService, Activity, Upload) {

        $scope.resetCurrentPageName("活动维护");
        $scope.utils = Food.utils;
        $scope.pageSize = 20;
        $scope.currentPage = 1;
        $scope.currentActivity = Activity.currentActivity;
        $scope.activityExtendList = Activity.activityExtendList = [];

        $scope.queryExtendList = function () {
            Activity.queryExtendList($scope.currentActivity.id, function () {
            });
        };
        $scope.queryExtendList();

        $scope.insertActivityExtend = function () {
            Activity.insertActivityExtend($scope.title, $scope.startTime, $scope.endTime, function () {

            });
        };
        $scope.deleteExtend = function (extendId) {
            Activity.deleteExtend(extendId, function () {
            });
        }
        //var operateTemplate = "<button class='btn  btn-sm' ng-click='grid.appScope.deleteExtend(row.entity.id)'>删除</button>";
        //$scope.gridOptions = {
        //    data: 'activityExtendList',
        //    enableColumnMenus: false,
        //    enableColumnResizing: true,
        //    rowHeight: 50,
        //    columnDefs: [
        //
        //        {
        //            field: 'contents',
        //            displayName: '内容详情',
        //            cellTemplate: '<div>{{row.entity.contents}}</div>'
        //        },
        //        {
        //            field: 'gmtCreate',
        //            displayName: '创建时间',
        //            cellFilter: "date:'yyyy-MM-dd hh:mm:ss'",
        //            width: "30%"
        //        },
        //        {
        //            field: 'id',
        //            displayName: '操作',
        //            width: "10%",
        //            cellTemplate: operateTemplate
        //        }
        //    ]
        //};

        $scope.$watch('files', function () {
            $scope.upload($scope.files);
        });
        $scope.log = '';
        $scope.isRunning = false;
        $scope.upload = function (files) {
            if (files && files.length) {
                $scope.isRunning = true;
                for (var i = 0; i < files.length; i++) {
                    var file = files[i];
                    var formData = new FormData();
                    formData.append("file", file);
                    formData.append("activityId", $scope.currentActivity.id);
                    $http.post('/azeroth/back/activity/insertExtend.htm', formData, {
                        transformRequest: function (data, headersGetterFunction) {
                            return data;
                        },
                        headers: {'Content-Type': undefined}
                    }).success(function (data, status) {
                        AlertService.addAlert({type: 'success', msg: '活动详情导入成功'}, 2000);
                        $window.location.href = "#activity/activityDetail";
                    }).error(function (data, status) {
                        AlertService.addAlert({type: 'success', msg: '活动详情导入失败'}, 2000);
                    });
                }
            }
        };


        $scope.exportTemplateExcel = function () {
            $window.location.href = "/azeroth/back/activity/exportTemplateExcel.htm?activityId=" + $scope.currentActivity.id;
        };


    }]);

